package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.File;
import org.rcloud.io.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/2/26.
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {
    @Autowired
    private FileService fileService;

    /*
   * 保存文件上传信息
   * */
    @ApiOperation(value="保存文件上传信息", notes="保存文件上传信息")
    @ResponseBody
    @RequestMapping(value = "/saveFile", produces = {"application/json;charset=UTF-8"})
    public ResultVo saveFile(@RequestBody String params){

        JSONObject object = JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());
        String eid = map.get("id").toString();
        String picture = map.get("picture").toString();
        String explain = map.get("explain").toString();

        Date t = new Date();
        Timestamp createData = new Timestamp(t.getTime());

        File file = new File();
        file.setEid(eid);
        file.setPicture(picture);
        file.setShouming(explain);
        file.setCreatedata(createData);
        file.setFlag(0);

        return  fileService.save(file);
    }

     /*
   * 根据eid查询图片
   * */
     @ApiOperation(value="根据eid查询图片", notes="根据eid查询图片")
     @ResponseBody
     @RequestMapping(value = "/findFile", produces = {"application/json;charset=UTF-8"})
     public PageVo<File> findFile(@RequestBody String params){

         String[] ids = params.split("=");
         return fileService.findFileByNum(ids[1]);
     }
    /*
    * 根据id修改图片编码
    * */
    @ApiOperation(value="根据id修改图片编码", notes="根据id修改图片编码")
    @ResponseBody
    @RequestMapping(value = "/updateFile", produces = {"application/json;charset=UTF-8"})
    public ResultVo UpdateFile(@RequestBody String params){
        String[] ids = params.split("=");
        int id=Integer.parseInt(ids[1]);
        return fileService.updateFile(id);

    }
 /*
    * 修改全部图片flag
    * */
    @ApiOperation(value="修改全部图片flag", notes="修改全部图片flag")
    @ResponseBody
    @RequestMapping(value = "/updateFlag", produces = {"application/json;charset=UTF-8"})
    public ResultVo UpdateFlag(@RequestBody String params){
        String[] ids = params.split("=");
        int id = Integer.parseInt(ids[1]);
        //根据id查询图片信息的eid
        File file = (File) fileService.findFileById(id).getInfo();
        String eid = file.getEid();

        //根据eid修改所有的flag
        fileService.updateFlagAll(eid);

        return fileService.updateFlag(id);

    }

}
